<template>
<el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="50%"
    @close="handleClose"
    
  >
    <el-form  ref="formRef" :model="form" label-width="100px" :rules="rules">
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="form.userId"></el-input>
    </el-form-item>
    <el-form-item label="用户名字" prop="userName">
      <el-input v-model="form.userName"></el-input>
    </el-form-item>
    <el-form-item label="用户邮箱" prop="userEmail">
      <el-input v-model="form.userEmail"></el-input>
    </el-form-item>
    <el-form-item label="用户电话" prop="userPhone">
      <el-input v-model="form.userPhone"></el-input>
    </el-form-item>
    <el-form-item label="用户权限" prop="userAdmin">
       <!-- <el-input v-model="form.userAdmin"></el-input> -->
      <el-radio-group v-model="form.userAdmin" class="ml-4">
          <el-radio label="不是" size="large">普通用户</el-radio>
          <el-radio label="是" size="large">管理员</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="用户性别" prop="userSex">
      <!-- <el-input v-model="form.userSex"></el-input> -->
      
      <el-radio-group v-model="form.userSex" class="ml-4">
          <el-radio label="男" size="large">男</el-radio>
          <el-radio label="女" size="large">女</el-radio>
      </el-radio-group>
    </el-form-item>
    
  </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>



<script setup>
import {defineEmits,ref,defineProps,watch} from 'vue';
import {$userAdd, $userUpdate} from '@/api/userapi'
import { ElMessage } from 'element-plus'
const props = defineProps({
  dialogTitle:{
    type:String,
    default:'',
    required:true
  },
  dialogTableValue:{
    type:Object,
    default:()=>{}
  }
})
const formRef=ref(null)



const form = ref({
  userId: "",
  userName: "",
  userPhone: "",
  userEmail: "",
  userSex: "",
  userAdmin:"",
  
})

watch (
  ()=>props.dialogTableValue,
  ()=>{
    
    form.value=props.dialogTableValue
  },
  {deep:true,immediate:true})

const emits =defineEmits(['update:model-value'],'initUserList')
const handleClose=()=>{
    emits('update:model-value',false)
}

const handleConfirm= ()=>{
    
    console.log(form.value,"dialog")
    formRef.value.validate(async(valid)=>{
      if(valid){
        if(props.dialogTitle==='添加用户'){
          alert("添加用户")
          const data=await $userAdd(form.value)
          console.log(data,"dialog")
          if(data.data=='新建用户成功'){
            ElMessage({
              message: '添加成功',
              type: 'success',
            })
          }else{
            ElMessage({
              message: '用户ID已存在',
              type: 'error',
            })
          }
        }else{
          alert("修改用户")
          const data=await $userUpdate(form.value)
          console.log(data,"dialog")
          if(data.data=='修改用户成功'){
            ElMessage({
              message: '修改成功',
              type: 'success',
            })
          }else{
            ElMessage({
              message: '修改失败',
              type: 'error',
            })
          }
        }
        
        emits('initUserList')
        handleClose()
      }else{
        return false
      }
    })   
    
}



const rules=ref({
    userId:[
        {
            required:true,
            message:'请输入id',
            trigger:'blur'
        },
        { type : "string" , required: true , pattern : /^[0-9]+$/ ,message:'ID只能由数字组成'},
         { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: ['blur', 'change'] }
    ],
    userName:[
        {
            required:true,
            message:'请输入姓名',
            trigger:'blur'
        },
        
        { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: ['blur', 'change'] }
    ],
    userPhone:[
        {
            required:true,
            message:'请输入电话',
            trigger:'blur'
        },
        { type : "string" , required: true , pattern : /^[0-9]+$/ ,len: 11,message:'请输入正确的电话'}
    ],
    userEmail:[
        {
            required:true,
            message:'请输入邮箱',
            trigger:'blur'
        },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ],
    userSex:[
        {
            required:true,
            message:'请输入性别',
            trigger:'blur'
        }
    ],
    userAdmin:[
        {
            required:true,
            message:'请选择权限',
            trigger:'blur'
        }
    ]
    
})
</script>

<style lang="scss" scoped>

</style>